package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.dto.TUserInfoDto;
import com.ruoyi.system.domain.dto.UserDTO;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TUserInfoMapper;
import com.ruoyi.system.domain.TUserInfo;
import com.ruoyi.system.service.ITUserInfoService;

import javax.annotation.Resource;

/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-16
 */
@Service
public class TUserInfoServiceImpl extends ServiceImpl<TUserInfoMapper,TUserInfo> implements ITUserInfoService
{
    @Resource
    private TUserInfoMapper tUserInfoMapper;

    /**
     * 查询用户信息
     *
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public TUserInfo selectTUserInfoById(Long id)
    {
        return tUserInfoMapper.selectTUserInfoById(id);
    }

    /**
     * 查询用户信息列表
     *
     * @param tUserInfo 用户信息
     * @return 用户信息
     */
    @Override
    public List<TUserInfo> selectTUserInfoList(TUserInfo tUserInfo)
    {
        return tUserInfoMapper.selectTUserInfoList(tUserInfo);
    }

    /**
     * 新增用户信息
     *
     * @param tUserInfo 用户信息
     * @return 结果
     */
    @Override
    public int insertTUserInfo(TUserInfo tUserInfo)
    {
        tUserInfo.setCreateTime(DateUtils.getNowDate());
        return tUserInfoMapper.insertTUserInfo(tUserInfo);
    }

    /**
     * 修改用户信息
     *
     * @param tUserInfo 用户信息
     * @return 结果
     */
    @Override
    public int updateTUserInfo(TUserInfo tUserInfo)
    {
        tUserInfo.setUpdateTime(DateUtils.getNowDate());
        return tUserInfoMapper.updateTUserInfo(tUserInfo);
    }

    /**
     * 批量删除用户信息
     *
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteTUserInfoByIds(Long[] ids)
    {
        return tUserInfoMapper.deleteTUserInfoByIds(ids);
    }

    /**
     * 删除用户信息信息
     *
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteTUserInfoById(Long id)
    {
        return tUserInfoMapper.deleteTUserInfoById(id);
    }

    @Override
    public TUserInfoDto getUserById(Long userId) {
        TUserInfoDto user  = tUserInfoMapper.selectByUserId(userId);
        return user;
    }

    @Override
    public TUserInfo selectByEmail(String userEmail) {
        TUserInfo info = tUserInfoMapper.selectByEmail(userEmail);
        return info;
    }

    @Override
    public TUserInfo selectByIosId(String appId) {
        TUserInfo info = tUserInfoMapper.selectByIosId(appId);
        return info;
    }

    @Override
    public TUserInfo selectByPhone(String phone) {
        TUserInfo info = tUserInfoMapper.selectByPhone(phone);
        return info;
    }

    @Override
    public TUserInfo selectByCode(String code) {
        TUserInfo info = tUserInfoMapper.selectByCode(code);
        return info;
    }

    @Override
    public List<UserDTO> selectByUserIds(List<Long> userIds) {
        List<UserDTO> userLists = tUserInfoMapper.selectByuserIds(userIds);
        return userLists;
    }

    @Override
    public List<UserDTO> selectNextUserList(Long userId, Integer level) {
        List<UserDTO> list = tUserInfoMapper.selectNextUserList(userId,level);
        return list;
    }

    @Override
    public Integer updateMoney(BigDecimal residueMoney, Long version, Long userId) {
        return tUserInfoMapper.updateMoney(residueMoney,version,userId);
    }

    @Override
    public List<TUserInfo> selectByParentIds(List<Long> userIds) {
        List<TUserInfo> list = tUserInfoMapper.selectByParentIds(userIds);
        return list;
    }

    @Override
    public void addMoney(BigDecimal money, Long version, Long userId) {
        Integer integer = tUserInfoMapper.addMoney(money, version, userId);
        if (integer<1){
            TUserInfo byId = getById(userId);
            addMoney(money, byId.getVersion(),userId);
            return;
        }
    }
}
